package com.ruoyi.email.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.email.domain.EmailLetter;
import com.ruoyi.email.mapper.EmailMailboxMapper;
import com.ruoyi.email.domain.EmailMailbox;
import com.ruoyi.email.service.IEmailMailboxService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 信箱Service业务层处理
 * 
 * @author cc
 * @date 2023-03-24
 */
@Service
public class EmailMailboxServiceImpl implements IEmailMailboxService 
{
    @Resource
    private EmailMailboxMapper emailMailboxMapper;

    /**
     * 查询信箱
     * 
     * @param id 信箱主键
     * @return 信箱
     */
    @Override
    public EmailMailbox selectEmailMailboxById(Long id)
    {
        return emailMailboxMapper.selectEmailMailboxById(id);
    }

    /**
     * 查询信箱列表
     * 
     * @param emailMailbox 信箱
     * @return 信箱
     */
    @Override
    public List<EmailMailbox> selectEmailMailboxList(EmailMailbox emailMailbox)
    {
        return emailMailboxMapper.selectEmailMailboxList(emailMailbox);
    }

    //查询发信箱
    @Override
    public List<EmailMailbox> selectByEmail(String email) {
        return emailMailboxMapper.selectByEmail(email);
    }

    /**
     * 新增信箱
     * 
     * @param emailMailbox 信箱
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEmailMailbox(EmailMailbox emailMailbox)
    {
        int rows = emailMailboxMapper.insertEmailMailbox(emailMailbox);
        insertEmailLetter(emailMailbox);
        return rows;
    }

    /**
     * 修改信箱
     * 
     * @param emailMailbox 信箱
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEmailMailbox(EmailMailbox emailMailbox)
    {
        emailMailboxMapper.deleteEmailLetterByEmailId(emailMailbox.getId());
        insertEmailLetter(emailMailbox);
        return emailMailboxMapper.updateEmailMailbox(emailMailbox);
    }

    /**
     * 批量删除信箱
     * 
     * @param ids 需要删除的信箱主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmailMailboxByIds(String ids)
    {
        emailMailboxMapper.deleteEmailLetterByEmailIds(Convert.toStrArray(ids));
        return emailMailboxMapper.deleteEmailMailboxByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除信箱信息
     * 
     * @param id 信箱主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmailMailboxById(Long id)
    {
        emailMailboxMapper.deleteEmailLetterByEmailId(id);
        return emailMailboxMapper.deleteEmailMailboxById(id);
    }

    /**
     * 新增邮件 信息
     * 
     * @param emailMailbox 信箱对象
     */
    public void insertEmailLetter(EmailMailbox emailMailbox)
    {
        List<EmailLetter> emailLetterList = emailMailbox.getEmailLetterList();
        Long id = emailMailbox.getId();
        if (StringUtils.isNotNull(emailLetterList))
        {
            List<EmailLetter> list = new ArrayList<EmailLetter>();
            for (EmailLetter emailLetter : emailLetterList)
            {
                emailLetter.setEmailId(id);
                list.add(emailLetter);
            }
            if (list.size() > 0)
            {
                emailMailboxMapper.batchEmailLetter(list);
            }
        }
    }
}
